﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml

Namespace Active.Modules.Forums.Controls
    <ToolboxData("<{0}:TopicsDisplay runat=server></{0}:TopicsDisplay>")> _
    Public Class TopicsDisplay
        Inherits ControlsBase
#Region "Private Members"
        Private MemberListMode As String
        Private ProfileVisibility As String
        Private UserNameDisplay As String
        Private DisableUserProfiles As Boolean
        Private bRead As Boolean = True
        Private bView As Boolean = True
        Dim topicsData As XmlDocument
#End Region
#Region "Event Handlers"
        Private Sub TopicsDisplay_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            Dim MainSettings As SettingsInfo = DataCache.MainSettings(ControlConfig.InstanceId)
            MemberListMode = MainSettings.MemberListMode
            ProfileVisibility = MainSettings.ProfileVisibility
            UserNameDisplay = MainSettings.UserNameDisplay
            DisableUserProfiles = MainSettings.DisableUserProfiles
            If topicsData Is Nothing Then
                Dim db As New Data.Topics
                topicsData = db.TopicsForDisplayXML(ControlConfig.SiteId, ControlConfig.InstanceId, -1, ForumUser.UserId, PageId, MainSettings.PageSize, ForumUser.IsSuperUser, "ReplyCreated", UserForumsList)

            End If
            Dim sTemp As String = String.Empty
            'pt = New Forums.Utils.TimeCalcItem("ForumDisplay")

            'TODO: Turn cache back on
            'Dim obj As Object = DataCache.CacheRetrieve(InstanceId & CurrentView)
            'If obj Is Nothing Then
            DisplayTemplate = Utilities.ParseTokenConfig(DisplayTemplate, "topic", ControlConfig)
            sTemp = ParseTemplate()
            'Else
            '   sTemp = CType(obj, String)
            'End If
            sTemp = Utilities.LocalizeControl(sTemp)
            'Security
            If Not sTemp.Contains(Globals.ControlRegisterAFTag) Then
                sTemp = Globals.ControlRegisterAFTag & sTemp
            End If
            sTemp = sTemp.Replace("[CREATEROLES]", "1;")
            sTemp = sTemp.Replace("[USERROLES]", ForumUser.UserRoles)
            sTemp = sTemp.Replace("[THEMEPATH]", ThemePath)
            sTemp = Utilities.ParseSecurityTokens(sTemp, ForumUser.UserRoles)
            Dim ctl As Control = Page.ParseControl(sTemp)
            LinkControls(ctl.Controls)
            Me.Controls.Add(ctl)


        End Sub
#End Region
#Region "Private Methods"
        Private Function ParseTemplate() As String
            Dim MainSettings As SettingsInfo = DataCache.MainSettings(ControlConfig.InstanceId)
            Dim sOut As String = DisplayTemplate
            'sOut = ParseForumData(sOut)
            Dim sort As String = SortColumns.ReplyCreated
            If sOut.Contains("[AF:SORT:TOPICCREATED]") Then
                sort = SortColumns.TopicCreated
                sOut = sOut.Replace("[AF:SORT:TOPICCREATED]", String.Empty)
            End If
            Dim Replies As Integer = 0
            Dim Views As Integer = 0
            Dim Subject As String = String.Empty
            Dim summary As String = String.Empty
            Dim body As String = String.Empty
            Dim replyCount As Integer = 0
            Dim viewCount As Integer = 0
            Dim topicid As Integer = 0
            Dim topicIcon As String = String.Empty
            Dim UserLastTopicRead As Integer = 0
            Dim UserLastReplyRead As Integer = 0
            Dim topicRating As Integer = 0
            Dim LastReplyId As Integer = 0
            Dim isLocked As Boolean = False
            Dim isPinned As Boolean = False
            Dim AuthorId As Integer = -1
            Dim AuthorName As String = String.Empty
            Dim AuthorFirstName As String = String.Empty
            Dim AuthorLastName As String = String.Empty
            Dim AuthorUserName As String = String.Empty
            Dim AuthorDisplayName As String = String.Empty
            Dim LastPostData As String = String.Empty
            Dim LastPostDate As Date
            Dim DateCreated As Date
            Dim sTopicURL As String = String.Empty
            Dim sBodyTitle As String = String.Empty
            Dim statusId As Integer = -1
            Dim isAnnounce As Boolean = False
            Dim AnnounceStart As Date
            Dim AnnounceEnd As Date
            Dim topicType As Integer = 0
            Dim la As Author = Nothing
            Dim ModApprove As String = String.Empty
            Dim ModDelete As String = String.Empty
            Dim ModEdit As String = String.Empty
            Dim ModPin As String = String.Empty
            Dim ModLock As String = String.Empty
            Dim ModMove As String = String.Empty
            Dim ModSplit As String = String.Empty
            Dim Reply As String = String.Empty
            Dim Lock As String = String.Empty
            Dim Pin As String = String.Empty
            Dim Delete As String = String.Empty
            Dim Edit As String = String.Empty
            Dim groupName As String = String.Empty
            'Dim t As New Data.Topics
            Dim topics As String = TemplateUtils.GetTemplateSection(sOut, "[TOPICS]", "[/TOPICS]")
            Dim sb As New Text.StringBuilder
            Dim xTopics As System.Xml.XmlNodeList

            xTopics = TopicsData.SelectNodes("//topics/topic")
            Dim rowCount As Integer = 0
            For Each xNode As System.Xml.XmlNode In xTopics
                Dim ForumName As String = String.Empty

                ForumId = Integer.Parse(xNode.Attributes("forumid").Value.ToString)
                ForumName = xNode.Item("forumname").InnerText
                groupName = xNode.Item("groupname").InnerText

                topicid = Integer.Parse(xNode.Attributes("topicid").Value.ToString)
                LastReplyId = Integer.Parse(xNode.Attributes("lastreplyid").Value.ToString)
                viewCount = Integer.Parse(xNode.Attributes("viewcount").Value.ToString)
                replyCount = Integer.Parse(xNode.Attributes("replycount").Value.ToString)
                isLocked = Boolean.Parse(xNode.Attributes("islocked").Value.ToString)
                isPinned = Boolean.Parse(xNode.Attributes("ispinned").Value.ToString)
                topicIcon = xNode.Attributes("topicicon").Value.ToString
                statusId = Integer.Parse(xNode.Attributes("statusid").Value.ToString)
                isAnnounce = Boolean.Parse(xNode.Attributes("isannounce").Value.ToString)
                AnnounceStart = CDate(xNode.Attributes("announcestart").Value.ToString)
                AnnounceEnd = CDate(xNode.Attributes("announceend").Value.ToString)
                topicType = Integer.Parse(xNode.Attributes("topictype").Value.ToString)
                AuthorId = Integer.Parse(xNode.Attributes("authorid").Value.ToString)
                DateCreated = CDate(xNode.Attributes("datecreated").Value.ToString)
                If Not String.IsNullOrEmpty(xNode.Attributes("lastpostdate").Value.ToString) Then
                    LastPostDate = CDate(xNode.Attributes("lastpostdate").Value.ToString)
                Else
                    LastPostDate = DateCreated
                End If
                UserLastReplyRead = Integer.Parse(xNode.Attributes("userlastreplyread").Value.ToString)
                UserLastTopicRead = Integer.Parse(xNode.Attributes("userlasttopicread").Value.ToString)
                topicRating = Integer.Parse(xNode.Attributes("topicrating").Value.ToString)
                Subject = xNode.Item("subject").InnerText
                Subject = Utilities.StripHTMLTag(Subject)
                Subject = Subject.Replace("&#91;", "[")
                Subject = Subject.Replace("&#93;", "]")
                summary = xNode.Item("summary").InnerText
                body = xNode.Item("body").InnerText
                AuthorName = xNode.Item("authorname").InnerText
                AuthorUserName = xNode.Item("username").InnerText
                AuthorFirstName = xNode.Item("firstname").InnerText
                AuthorLastName = xNode.Item("lastname").InnerText
                AuthorDisplayName = xNode.Item("displayname").InnerText
                Dim secNode As XmlNode = xNode.Item("security")
                ModApprove = secNode.Item("modapprove").InnerText
                ModDelete = secNode.Item("moddelete").InnerText
                ModEdit = secNode.Item("modedit").InnerText
                ModPin = secNode.Item("modpin").InnerText
                ModLock = secNode.Item("modlock").InnerText
                ModMove = secNode.Item("modmove").InnerText
                Reply = secNode.Item("reply").InnerText
                Lock = secNode.Item("lock").InnerText
                Pin = secNode.Item("pin").InnerText
                Delete = secNode.Item("delete").InnerText
                Edit = secNode.Item("edit").InnerText
                If Not xNode.Item("lastpost") Is Nothing Then
                    LastPostData = xNode.Item("lastpost").OuterXml
                Else
                    LastPostData = String.Empty
                End If

                If Not String.IsNullOrEmpty(LastPostData) Then
                    la = New Author
                    Dim xDoc As New System.Xml.XmlDocument
                    xDoc.LoadXml(LastPostData)
                    Dim xLast As System.Xml.XmlNode = xDoc.SelectSingleNode("//lastpost")
                    If Not xLast Is Nothing Then
                        With la
                            .AuthorId = Integer.Parse(xLast.Item("authorid").InnerText.ToString)
                            .DisplayName = xLast.Item("displayname").InnerText
                            .FirstName = xLast.Item("firstname").InnerText
                            .LastName = xLast.Item("lastname").InnerText
                            .Username = xLast.Item("username").InnerText
                        End With
                    End If
                End If
                Dim BodyLength As Integer = -1
                Dim BodyTrim As String = ""
                sTopicURL = URL.ForTopic(TabId, PortalId, ForumId, topicid, groupName, ForumName, Subject, 1)
                Dim tmp As String = topics
                Dim sLastPost As String = TemplateUtils.GetTemplateSection(tmp, "[LASTPOST]", "[/LASTPOST]")
                If Not la Is Nothing Then
                    sLastPost = sLastPost.Replace("[LASTPOSTDISPLAYNAME]", UserProfiles.GetDisplayName(ModuleId, ProfileVisibility, False, la.AuthorId, UserNameDisplay, DisableUserProfiles, la))
                    sLastPost = sLastPost.Replace("[AF:PROFILE:AVATAR]", UserProfiles.GetAvatar(la.AuthorId, PortalId, ThemePath, MainSettings.Theme, String.Empty, 0, MainSettings.AvatarWidth, MainSettings.ProfileType, MainSettings.AvatarDefault))

                    sLastPost = sLastPost.Replace("[LASTPOSTDATE]", Utilities.GetDate(LastPostDate, ModuleId))
                    If bRead Then
                        'TODO: Replace with link
                        sLastPost = sLastPost.Replace("[AF:ICONLINK:LASTREPLY]", String.Empty)
                    Else
                        sLastPost = sLastPost.Replace("[AF:ICONLINK:LASTREPLY]", String.Empty)
                    End If
                    tmp = TemplateUtils.ReplaceSubSection(tmp, sLastPost, "[LASTPOST]", "[/LASTPOST]")
                Else
                    tmp = TemplateUtils.ReplaceSubSection(tmp, String.Empty, "[LASTPOST]", "[/LASTPOST]")
                End If

                'Dim sLastReplyURL As String = NavigateUrl(TabId, "", New String() {ParamKeys.ForumId & "=" & ForumId, ParamKeys.TopicId & "=" & topicid, ParamKeys.ViewType & "=topic", ParamKeys.ContentJumpId & "=" & LastReplyId})
                Dim sLastReadURL As String = String.Empty
                Dim sUserJumpUrl As String = String.Empty
                'If UserLastReplyRead > 0 Then
                'sLastReadURL = NavigateUrl(TabId, "", New String() {ParamKeys.ForumId & "=" & ForumId, ParamKeys.TopicId & "=" & topicid, ParamKeys.ViewType & "=topic", ParamKeys.FirstNewPost & "=" & UserLastReplyRead})
                'End If

                'If UserPrefJumpLastPost And sLastReadURL <> String.Empty Then
                '    '   sTopicURL = sLastReadURL
                '    sUserJumpUrl = sLastReadURL
                'End If
                If tmp.Contains("[BODY:") Then
                    Dim inStart As Integer = InStr(tmp, "[BODY:") + 5
                    Dim inEnd As Integer = InStr(inStart, tmp, "]") - 1
                    Dim sLength As String = tmp.Substring(inStart, inEnd - inStart)
                    BodyLength = CType(sLength, Integer)
                    BodyTrim = "[BODY:" & BodyLength.ToString & "]"
                End If
                If Not BodyTrim = String.Empty Then
                    Dim BodyPlain As String = body.Replace("<br>", vbCrLf)
                    BodyPlain = BodyPlain.Replace("<br />", vbCrLf)
                    BodyPlain = Utilities.StripHTMLTag(BodyPlain)
                    If BodyLength > 0 And BodyPlain.Length > BodyLength Then
                        BodyPlain = BodyPlain.Substring(0, BodyLength)
                    End If
                    BodyPlain = BodyPlain.Replace(vbCrLf, "<br />")
                    tmp = tmp.Replace(BodyTrim, BodyPlain)
                End If
                If isLocked Then
                    tmp = tmp.Replace("[RESX:LockTopic]", "[RESX:UnLockTopic]")
                    tmp = tmp.Replace("[RESX:Confirm:Lock]", "[RESX:Confirm:UnLock]")
                End If
                If isPinned Then
                    tmp = tmp.Replace("[RESX:PinTopic]", "[RESX:UnPinTopic]")
                    tmp = tmp.Replace("[RESX:Confirm:Pin]", "[RESX:Confirm:UnPin]")
                End If
                tmp = tmp.Replace("[BODY]", GetBody(body, AuthorId))
                tmp = tmp.Replace("[BODYTITLE]", GetTitle(body, AuthorId))
                tmp = tmp.Replace("[AUTHORID]", AuthorId.ToString)
                tmp = tmp.Replace("[USERID]", UserId.ToString)
                If String.IsNullOrEmpty(Subject) Then
                    Subject = "(no subject)"
                End If
                Dim LastSubject As String = Subject
                If LastReplyId > 0 Then
                    LastSubject = "[RESX:RE]" & LastSubject
                End If

                tmp = tmp.Replace("[SUBJECT]", Subject)
                Dim sSubjectLink As String = GetTopic(topicid, Subject, sBodyTitle, UserId, AuthorId, replyCount, -1, sUserJumpUrl, groupName, ForumName)
                Dim lastSubjectLink As String = GetTopic(topicid, Subject, String.Empty, UserId, AuthorId, replyCount, -1, sUserJumpUrl, groupName, ForumName)
                tmp = tmp.Replace("[SUBJECTLINK]", sSubjectLink)
                tmp = tmp.Replace("[THEMEPATH]", ThemePath)
                tmp = tmp.Replace("[TOPICID]", topicid.ToString)
                tmp = tmp.Replace("[MODEDIT]", String.Empty)
                tmp = tmp.Replace("[MODDELETE]", String.Empty)
                tmp = tmp.Replace("[MODMOVE]", String.Empty)
                tmp = tmp.Replace("[MODLOCK]", String.Empty)
                tmp = tmp.Replace("[MODPIN]", String.Empty)
                tmp = tmp.Replace("[REPLIES]", replyCount.ToString)
                tmp = tmp.Replace("[VIEWS]", viewCount.ToString)
                tmp = tmp.Replace("[POSTICONIMAGE]", GetIcon(UserLastTopicRead, UserLastReplyRead, topicid, LastReplyId, topicIcon, isPinned, isLocked))

                tmp = tmp.Replace("[POSTICONCSS]", GetIconCSS(UserLastTopicRead, UserLastReplyRead, topicid, LastReplyId, topicIcon, isPinned, isLocked))
                If tmp.Contains("[STATUSCSS]") Then
                    Dim sImg As String = String.Empty
                    If statusId = -1 Then
                        tmp = tmp.Replace("[STATUSCSS]", String.Empty)
                    Else
                        sImg = "<span title=""[RESX:PostStatus" & statusId.ToString & "]"" class=""afstatus afstatus" & statusId.ToString & """></span>"
                    End If
                    tmp = tmp.Replace("[STATUSCSS]", sImg)
                End If
                tmp = tmp.Replace("[STARTEDBY]", UserProfiles.GetDisplayName(ModuleId, ProfileVisibility, False, AuthorId, UserNameDisplay, DisableUserProfiles, AuthorUserName, AuthorFirstName, AuthorLastName, AuthorDisplayName))
                tmp = tmp.Replace("[DATECREATED]", Utilities.GetDate(DateCreated, ModuleId))
                tmp = tmp.Replace("[TOPICURL]", sTopicURL)
                'TODO: Still need to process
                tmp = tmp.Replace("[AF:ICONLINK:LASTREAD]", String.Empty)
                tmp = tmp.Replace("[AF:URL:LASTREAD]", String.Empty)
                If tmp.Contains("[STATUS]") Then
                    Dim sImg As String = String.Empty
                    If statusId = -1 Then
                        tmp = tmp.Replace("[STATUS]", String.Empty)
                    Else
                        sImg = "<img alt=""[RESX:PostStatus" & statusId.ToString & "]"" src=""" & Utilities.AppPath & "themes/images/status" & statusId.ToString & ".png"" />"
                    End If
                    tmp = tmp.Replace("[STATUS]", sImg)
                End If


                'sTopicsTemplate = sTopicsTemplate.Replace("[AF:UI:MINIPAGER]", GetSubPages(TabId, ModuleId, ReplyCount, ForumId, TopicId))
                tmp = tmp.Replace("[AF:UI:MINIPAGER]", String.Empty)
                tmp = tmp.Replace("[POSTRATINGDISPLAY]", String.Empty)
                tmp = tmp.Replace("[TOPICRATING]", topicRating.ToString)
                tmp = tmp.Replace("[ROWCSS]", GetRowCSS(UserLastTopicRead, UserLastReplyRead, topicid, LastReplyId, rowCount))

                tmp = tmp.Replace("[AF:TOPIC:FRIENDLYDATE]", Utilities.HumanFriendlyDate(DateCreated, ModuleId))
                tmp = tmp.Replace("[AF:REPLY:FRIENDLYDATE]", Utilities.HumanFriendlyDate(LastPostDate, ModuleId))
                tmp = tmp.Replace("[AF:TOPIC:FORUMNAME]", ForumName)
                tmp = tmp.Replace("[AF:TOPIC:LASTSUBJECT:LINK]", lastSubjectLink)
                tmp = tmp.Replace("[AF:TOPIC:LASTSUBJECT]", LastSubject)
                tmp = tmp.Replace("[AF:TOPIC:FORUMNAME:LINK]", "<a href=""" & URL.ForForum(PageId, ForumId, GroupName(ForumId), ForumName) & """ title=""" & ForumName & """>" & ForumName & "</a>")
                tmp = tmp.Replace("AF:SECURITY:MODROLES]", "AF:SECURITY:MODROLES:" & ModApprove & "]")
                tmp = tmp.Replace("AF:SECURITY:MODAPPROVE]", "AF:SECURITY:MODAPPROVE:" & ModApprove & "]")
                tmp = tmp.Replace("AF:SECURITY:DELETE]", "AF:SECURITY:DELETE:" & Delete & ModDelete & "]")
                tmp = tmp.Replace("AF:SECURITY:EDIT]", "AF:SECURITY:EDIT:" & Edit & ModEdit & "]")
                tmp = tmp.Replace("AF:SECURITY:LOCK]", "AF:SECURITY:LOCK:" & Lock & ModLock & "]")
                tmp = tmp.Replace("AF:SECURITY:MOVE]", "AF:SECURITY:MOVE:" & ModMove & "]")
                tmp = tmp.Replace("AF:SECURITY:PIN]", "AF:SECURITY:PIN:" & Pin & ModPin & "]")
                tmp = tmp.Replace("AF:SECURITY:SPLIT]", "AF:SECURITY:SPLIT:" & ModSplit & "]")
                tmp = tmp.Replace("AF:SECURITY:REPLY]", "AF:SECURITY:REPLY:" & Reply & "]")

                sb.Append(tmp)
                rowCount += 1
            Next

            sOut = TemplateUtils.ReplaceSubSection(sOut, sb.ToString, "[TOPICS]", "[/TOPICS]")
            sOut = sOut.Replace("[TOPICS]", String.Empty)
            sOut = sOut.Replace("[/TOPICS]", String.Empty)

            DataCache.CacheStore(ModuleId & "topicsview", sOut)
            Return sOut
        End Function
        Private Function GetTitle(ByVal Body As String, ByVal AuthorId As Integer) As String
            If bRead Or AuthorId = Me.UserId Then
                Body = Replace(Body, "<br>", vbCrLf)
                Body = Replace(Body, "[", "&#91")
                Body = Replace(Body, "]", "&#93")
                Body = Utilities.StripHTMLTag(Body)
                Body = Left(Body, 500) & "..."
                Body = Replace(Body, """", "'")
                Body = Replace(Body, "?", String.Empty)
                Body = Replace(Body, "+", String.Empty)
                Body = Replace(Body, "%", String.Empty)
                Body = Replace(Body, "#", String.Empty)
                Body = Replace(Body, "@", String.Empty)
                Return Body.Trim
            Else
                Return String.Empty
            End If

        End Function
        Private Function GetBody(ByVal Body As String, ByVal AuthorId As Integer) As String
            If bRead Or AuthorId = Me.UserId Then
                Body = Replace(Body, "[", "&#91")
                Body = Replace(Body, "]", "&#93")
                Return Body
            Else
                Return String.Empty
            End If

        End Function
        Private Function GetTopic(ByVal TopicId As Integer, ByVal Subject As String, ByVal BodyTitle As String, ByVal UserID As Integer, ByVal PostUserID As Integer, ByVal Replies As Integer, ByVal ForumOwnerID As Integer, ByVal sLink As String, ByVal GroupName As String, ByVal Forumname As String) As String
            Dim sOut As String
            Subject = Utilities.StripHTMLTag(Subject)
            Subject = Subject.Replace("""", String.Empty)
            Subject = Subject.Replace("'", String.Empty)
            Subject = Subject.Replace("#", String.Empty)
            Subject = Subject.Replace("%", String.Empty)
            ' Subject = Subject.Replace("?", String.Empty)
            Subject = Subject.Replace("+", String.Empty)

            If bRead Then
                If sLink = String.Empty Then
                    sOut = "<a title=""" & BodyTitle & """ href=""" & URL.ForTopic(TabId, PortalId, ForumId, TopicId, GroupName, Forumname, Subject, 1) & """>" & Subject & "</a>"
                Else
                    sOut = "<a title=""" & BodyTitle & """ href=""" & sLink & """>" & Subject & "</a>"
                End If

                'sOut = sOut & GetSubPages(TabID, ModuleID, Replies, ForumID, PostID)
            Else
                sOut = Subject
            End If
            Return sOut
        End Function
        Private Sub LinkControls(ByVal ctrls As ControlCollection)
            For Each ctrl As Control In ctrls
                If TypeOf ctrl Is Controls.ControlsBase Then
                    DirectCast(ctrl, Controls.ControlsBase).ControlConfig = Me.ControlConfig
                    DirectCast(ctrl, Controls.ControlsBase).ModuleConfiguration = Me.ModuleConfiguration
                End If
                If ctrl.Controls.Count > 0 Then
                    LinkControls(ctrl.Controls)
                End If
            Next
        End Sub

        Private Function GetIcon(ByVal LastTopicRead As Integer, ByVal LastReplyRead As Integer, ByVal TopicId As Integer, ByVal ReplyId As Integer, ByVal Icon As String, Optional ByVal Pinned As Boolean = False, Optional ByVal Locked As Boolean = False) As String
            If Icon = String.Empty Then
                If Pinned And Locked Then
                    Return "topic_pinlocked.png"

                ElseIf Pinned Then
                    Return "topic_pin.png"

                ElseIf Locked Then
                    Return "lock.gif"

                Else
                    If Not HttpContext.Current.Request.IsAuthenticated Then
                        Return "document.gif"

                    Else
                        If LastTopicRead = 0 Then
                            Return "document_new.gif"

                        ElseIf LastTopicRead > 0 And LastReplyRead < ReplyId Then
                            Return "document_new.gif"

                        Else
                            Return "document.gif"
                        End If
                    End If

                End If
            Else
                Return Icon
            End If
        End Function
        Private Function GetIconCSS(ByVal LastTopicRead As Integer, ByVal LastReplyRead As Integer, ByVal TopicId As Integer, ByVal ReplyId As Integer, ByVal Icon As String, Optional ByVal Pinned As Boolean = False, Optional ByVal Locked As Boolean = False) As String
            Dim sb As New StringBuilder
            If String.IsNullOrEmpty(Icon) Then
                sb.Append("<div class=""docicon"">")
            Else
                sb.Append("<div class=""docicon"" style=""background-image:none;""><img src=""[EMOTICONPATH]/" & Icon & """ border=""0"" />")
            End If

            If Pinned Then
                sb.Append("<div class=""aficonover dociconpin""></div>")
            End If
            If Locked Then
                sb.Append("<div class=""aficonover dociconlock""></div>")
            End If
            If HttpContext.Current.Request.IsAuthenticated Then
                If LastTopicRead = 0 Then
                    sb.Append("<div class=""aficonover dociconnew""></div>")
                ElseIf LastTopicRead > 0 And LastReplyRead < ReplyId Then
                    sb.Append("<div class=""aficonover dociconnew""></div>")
                    'Else
                    '    'Return "document.gif"
                    '    Return "<div class=""docicon"">[STATUSCSS]<div class=""aficonover dociconnorm""></div></div>"
                End If
            End If
            'If Pinned And Locked Then
            '    'Return "topic_pinlocked.png"
            '        sb.Append("<div class=""aficonover dociconpin""></div><div class=""aficonover dociconlock""></div></div>"
            'ElseIf Pinned Then
            '    'Return "topic_pin.png"
            '    Return "<div class=""docicon"">[STATUSCSS]</div>"
            'ElseIf Locked Then
            '    'Return "lock.gif"
            '    Return "<div class=""docicon"">[STATUSCSS]</div>"
            'Else
            '    If Not HttpContext.Current.Request.IsAuthenticated Then
            '        'Return "document.gif"
            '        Return "<div class=""docicon"">[STATUSCSS]<div class=""aficonover dociconnorm""></div></div>"
            '    Else
            '        If LastTopicRead = 0 Then
            '            'Return "document_new.gif"
            '            Return "<div class=""docicon"">[STATUSCSS]<div class=""aficonover dociconnew""></div></div>"
            '        ElseIf LastTopicRead > 0 And LastReplyRead < ReplyId Then
            '            'Return "document_new.gif"
            '            Return "<div class=""docicon"">[STATUSCSS]<div class=""aficonover dociconnew""></div></div>"
            '        Else
            '            'Return "document.gif"
            '            Return "<div class=""docicon"">[STATUSCSS]<div class=""aficonover dociconnorm""></div></div>"
            '        End If
            '    End If

            'End If

            sb.Append("[STATUSCSS]</div>")
            Return sb.ToString
        End Function
        Private Function GetRowCSS(ByVal LastTopicRead As Integer, ByVal LastReplyRead As Integer, ByVal TopicId As Integer, ByVal ReplyId As Integer, ByVal RowCount As Integer) As String
            Dim isRead As Boolean = False
            If LastTopicRead >= TopicId And LastReplyRead >= ReplyId Then
                isRead = True
            End If
            If Not HttpContext.Current.Request.IsAuthenticated Then
                isRead = True
            End If
            If isRead = True Then
                If RowCount Mod 2 = 0 Then
                    Return "aftopicrow"
                Else
                    Return "aftopicrowalt"
                End If
            Else
                If RowCount Mod 2 = 0 Then
                    Return "aftopicrownew"
                Else
                    Return "aftopicrownewalt"
                End If
            End If
        End Function
#End Region


    End Class

End Namespace
